package com.feri.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feri.api.dao.CommentDao;
import com.feri.api.dao.MoodDao;
import com.feri.api.dao.MoodlikeDao;
import com.feri.api.dto.MoodAddDto;
import com.feri.api.dto.MoodPageDto;
import com.feri.api.service.intf.CommentService;
import com.feri.api.service.intf.MoodService;
import com.feri.api.service.intf.MoodlikeService;
import com.feri.api.util.BaiduCensorUtil;
import com.feri.api.util.TokenUtil;
import com.feri.api.vo.PageVo;
import com.feri.api.vo.R;
import com.feri.common.entity.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @author lr
 * @date 2022/10/3
 */
@Service
public class MoodServiceImpl implements MoodService {
    private final MoodDao dao;
    private final MoodlikeDao likedao;
    private final CommentDao commentDao;
    public MoodServiceImpl(MoodDao dao,MoodlikeDao likedao,CommentDao commentDao){
        this.dao=dao;
       this.likedao=likedao;
       this.commentDao=commentDao;
    }


    @Override
    public R save(MoodAddDto dto,String token) {
        Long a= TokenUtil.toLong(token);
        if (dto!=null&&a>0){
            if (dto.getType()==null){
                dto.setType(0);
            }
            Mood mood = new Mood(a,dto.getType(),dto.getInfo(),1,null);
            if (BaiduCensorUtil.textCensor(dto.getInfo())){
              if (dao.insert(mood)>0){
                  return R.ok();
              }
            }else {
                mood.setFlag(0);
                dao.insert(mood);
                return R.fail("您输入的内容包含违禁词，请重新输入");
            }
        }
        return R.fail();
    }

    @Override
    public R queryAll(MoodPageDto dto) {
        QueryWrapper<Mood> moodQueryWrapper = new QueryWrapper<>();
        if (dto.getType()==null){
            dto.setType(0);
        }
        moodQueryWrapper.eq("type",dto.getType()).orderByDesc("ctime");
        Page<Mood> page=dao.selectPage((new Page<>(dto.getPage(),dto.getSize())),moodQueryWrapper);
        return R.ok(new PageVo<Mood>(page.getTotal(),page.getPages(),page.getRecords()));

    }

    @Override
    public R forward(Long id, String token) {
        Long a=TokenUtil.toLong(token);
        if (id>0&&a>0){
            Mood mood = dao.selectById(id);
            if (mood!=null){
                Mood mood1 = new Mood(a,mood.getType(), mood.getInfo(), 1,mood.getId());
                if (dao.insert(mood1)>0){
                    return R.ok();
                }
            }

        }
        return R.fail();
    }

    @Override
    @Transactional
    public R delete(Long id, String token) {
        Long a=TokenUtil.toLong(token);
        if (id>0&&a>0){
            Mood mood = dao.selectById(id);
            if (mood!=null){
                if (mood.getUid()==a){
                   dao.deleteById(id);
                   dao.delete(new QueryWrapper<Mood>().eq("pid",id));
                   likedao.delete(new QueryWrapper<Moodlike>().eq("mid",id));
                   commentDao.delete(new QueryWrapper<Comment>().eq("mid",id));
                  return R.ok();

                }

            }

        }
        return R.fail();
    }

    @Override
    public R queryMy(String token) {
        Long a=TokenUtil.toLong(token);
        if (a>0){
            return R.ok(dao.selectList(new QueryWrapper<Mood>().eq("uid",a).orderByDesc("ctime")));
        }
        return R.fail();
    }

    @Override
    public R search(String info) {
        if (StringUtils.hasLength(info)){
            QueryWrapper<Mood> queryWrapper=new QueryWrapper<>();
            queryWrapper.like("info",info).orderByDesc("ctime");
            Page<Mood> page=dao.selectPage((new Page<>(0,30)),queryWrapper);
            return R.ok(page.getRecords());
        }
        return null;
    }
}
